<template>
    <div>
        <div class="leftList comm-list" v-infinite-scroll="load" :style="{ height: stateHooks.leftHeight - 14 + 'px' }">
            <div class="item-box" v-for="(item, i_index) in listData" :key="'_left_list' + i_index"
                @click="clickItem(i_index)" :class="listI === i_index ? 'acitve' : ''" infinite-scroll-distance="100">
                <div>
                    <div v-for="(row, i_cell) in templates" :key="'_row' + i_cell">
                        <p :class="row.class" v-if="row && row.type == 'init'">{{ item[row.key] }}</p>
                        <slot :row="item" v-if="row && row.type == 'slot'" :name="row.key"></slot>
                    </div>
                </div>
            </div>
            <p class="flex_center font_black9 mb20" v-if="lLoading">
                <i class="el-icon-loading" style="font-size: 16px;"></i>
                <span class="ml5"> 加载中...</span>
            </p>
            <p class="flex_center font_black9 mb20" v-if="!lLoading && lNoMore">没有更多了</p>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, defineProps, defineEmits, watch } from '@vue/composition-api'
import { useCommonHooks } from '@/hooks/useCommonHooks.js'
// 使用全局状态
const { stateHooks } = useCommonHooks()

const props = defineProps({
    templates: {
        type: Array,
        required: true
    },
    listData: {
        type: Array,
        required: true
    },
    selectedIndex: {
        type: Number,
        default: 0,
        required: false
    },
    lLoading: {
        type: Boolean,
        default: false
    },
    lNoMore: {
        type: Boolean,
        default: false
    },
})

const emit = defineEmits(['update:selectedIndex', 'loadFun'])
const listI = ref(0)
watch(() => props?.selectedIndex, (newValue) => {
    if (newValue != null) {
        listI.value = newValue
    } else {
        listI.value = null
    }
})

const clickItem = (index) => {
    if (listI.value == index) {
        return
    }
    emit('update:selectedIndex', index)
    emit('clickItem', index)
}

const load = () => {
    if (props?.lNoMore) return
    setTimeout(() => {
        emit('loadFun')
    }, 300);
}

</script>

<style lang="scss" scoped>
// 定义颜色变量，便于统一管理和维护
$primary-color: #3c86f1;
$hover-color: #f0f0ff;
$active-color: #3c86f1;
$text-primary: #333;
$text-secondary: #666;
$text-tertiary: #999;
$border-color: #e8e8e8;
$background-color: #fff;
$shadow-light: 0 2px 4px rgba(0, 0, 0, 0.06);
$shadow-hover: 0 2px 4px rgba(0, 0, 0, 0.1);

// 重置样式
* {
    box-sizing: border-box;
}

.leftList {
    box-sizing: border-box;
    padding: 0 10px 10px 0;
    font-size: 14px;
    margin-top: 10px;
    height: calc(100vh - 40px - 15px - 10px - 40px - 10px);
    overflow-y: auto;

    // 自定义滚动条
    &::-webkit-scrollbar {
        width: 6px;
        height: 6px;
    }

    &::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 3px;
    }

    &::-webkit-scrollbar-thumb {
        background: #c1c1c1;
        border-radius: 3px;
    }

    &::-webkit-scrollbar-thumb:hover {
        background: #a8a8a8;
    }
}

.item-box {
    width: 100%;
    padding: 10px 15px 6px;
    border-radius: 8px;
    background: $background-color;
    margin-bottom: 6px;
    position: relative;
    cursor: pointer;
    box-shadow: $shadow-light;
    transition: all 0.3s ease;
    border: 1px solid $border-color;

    // 卡片悬浮效果
    &:hover {
        background: $hover-color;
        transform: translateY(-2px);
        box-shadow: $shadow-hover;
        border-color: $primary-color;
    }

    // 列表项内边距
    >div {
        >div {
            margin-bottom: 5px;

            &:last-child {
                margin-bottom: 0;
            }
        }
    }

    // 二级文字样式
    .ps {
        color: $text-tertiary;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

// 选中状态样式
.acitve {
    background: $active-color !important;
    color: #fff;
    border-color: $active-color;
    transform: translateY(-1px);
    box-shadow: 0 3px 6px rgba(90, 90, 255, 0.3);

    .ps {
        color: rgba(255, 255, 255, 0.8);
    }

    // 为选中的卡片添加一个标记
    //   &::after {
    //     content: '';
    //     position: absolute;
    //     right: 10px;
    //     top: 50%;
    //     transform: translateY(-50%);
    //     width: 6px;
    //     height: 6px;
    //     border-radius: 50%;
    //     background: #fff;
    //   }
}

// 数据项分隔线样式
.num_od {
    border-top: 1px solid #ede7e7;
    height: 40px;
    line-height: 40px;
    margin-top: 6px;
    padding-top: 6px;
}

// 加载状态样式优化
.flex_center {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
    color: $text-tertiary;

    .el-icon-loading {
        animation: rotating 1s linear infinite;
    }

    .ml5 {
        margin-left: 5px;
    }
}

// 辅助工具类
.font_black9 {
    font-size: 12px;
    color: $text-tertiary;
}

.mb20 {
    margin-bottom: 20px;
}

// 自定义动画
@keyframes rotating {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

// 响应式适配
@media (max-width: 1200px) {
    .leftList {
        padding: 0 8px 8px 0;
    }

    .item-box {
        padding: 12px;
        margin-bottom: 8px;
    }
}
</style>